/**
    Copyright (C) 2012 Guido Ludwig

    This file is part of JATT.
	http://code.google.com/p/jatt/

    JATT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.guidoludwig.jatt.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.activation.MimetypesFileTypeMap;
import javax.imageio.ImageIO;

public class ImageInfo {

//	image png tif jpg jpeg bmp
//	text  txt text rtf TXT
//	# either place those lines within the files specified by 2-4 or
//
//	MimetypesFileTypeMap mtftp = new MimetypesFileTypeMap();
//	mtftp.add("image png tif jpg jpeg bmp")

	private static final MimetypesFileTypeMap mtftp = new MimetypesFileTypeMap();
	public static final String MIME_TYPE_JPG = "image/jpeg";
	public static final String MIME_TYPE_PNG = "image/png";
	static {
		mtftp.addMimeTypes(MIME_TYPE_JPG + " jpg jpeg JPG JPEG");
		mtftp.addMimeTypes(MIME_TYPE_PNG + " png PNG");
		mtftp.addMimeTypes("image/gif gif GIF");
		mtftp.addMimeTypes("image/tiff tif tiff TIF TIFF");
		mtftp.addMimeTypes("image/bmp bmp BMP");
	}

	private final File file;
	private int width;
	private long size;
	private BufferedImage bufferedImage;

	public ImageInfo(File file) {
		if (file == null) {
			throw new IllegalArgumentException("File must not be null");
		}
		if (!file.exists()) {
			throw new IllegalArgumentException("File does not exist" + file.getAbsolutePath());
		}
		this.file = file;
		width = -1;
		size = -1;
	}

	public File getFile() {
		return file;
	}

	public int getWidth() {
		if (width == -1) {
			try {
				width = getBufferedImage().getWidth();
			} catch (IOException e) {
				// Size can't be determined
				width = 0;
			}
		}
		return width;
	}

	public long getSize() {
		if (size == -1) {
			size = file.length();
		}
		return size;
	}

	public String getExtension() {
		int lastDot = file.getAbsolutePath().lastIndexOf('.');
		if (lastDot < 0) {
			return null;
		}
		return file.getAbsolutePath().substring(lastDot +1);
	}

	public String getAbsoluteBaseName() {
		int lastDot = file.getAbsolutePath().lastIndexOf('.');
		if (lastDot < 0) {
			return file.getAbsolutePath();
		}
		return file.getAbsolutePath().substring(0,lastDot);
	}
	
	public String getBaseName() {
		int lastDot = file.getName().lastIndexOf('.');
		if (lastDot < 0) {
			return file.getName();
		}
		return file.getName().substring(0,lastDot);
	}

	public String getMimeType() {
		return mtftp.getContentType(file);
	}

	public boolean supportsCompression() {
		return MIME_TYPE_JPG.equals(getMimeType());
	}

	public boolean isCompressionRecommended(int maxWidth, long maxSize) {
		if (getWidth() > maxWidth) {
			return true;
		}
		if (getSize() > maxSize && supportsCompression()) {
			return true;
		}
		return false;
	}


	public BufferedImage getBufferedImage() throws IOException {
		if (bufferedImage == null) {
			bufferedImage = ImageIO.read(file);
		}
		return bufferedImage;
	}

}